package com.stcc.dataConfig.impl.ecommerce;

import com.alibaba.fastjson.JSONObject;
import com.stcc.base.util.HttpUrlConnectionUtil;
import com.stcc.dataConfig.bo.ecommerce.JingDongLoginReqBO;
import com.stcc.dataConfig.bo.ecommerce.JingDongLoginRspBO;
import com.stcc.dataConfig.bo.ecommerce.QryAllEcommerceRspBO;
import com.stcc.dataConfig.dao.ecommerce.EcommerceDao;
import com.stcc.dataConfig.service.ecommerce.JingDongLoginService;
import com.stcc.dataConfig.vo.ecommerce.EcommerceVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述:JingDongLoginService
 * 项目名:stcc_gl-parent
 * 包名:com.stcc.dataConfig.impl.jingDongImpl
 * 作者:stcc
 * 创建时间:2021/4/22 11:17
 * 版本:1.0
 */
@Component
@DubboService
public class JingDongLoginServiceImpl implements JingDongLoginService {
    final static String headerAgent = "User-Agent";
    final static String headerAgentArg = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36";
    final static String Referer = "Referer";
    final static String RefererArg = "https://passport.jd.com/new/login.aspx";

    @Autowired
    private EcommerceDao ecommerceDao;
    @Override
    public QryAllEcommerceRspBO jdlogin(JingDongLoginReqBO reqBO) {
        QryAllEcommerceRspBO rspBO = new QryAllEcommerceRspBO();
        EcommerceVO voParam = new EcommerceVO();
        List<JingDongLoginRspBO> jdRspBOList = new ArrayList<>();
        voParam.setEcommerceType(reqBO.getEcommerceType());
        List<EcommerceVO> voList = ecommerceDao.findAll(voParam);
        JSONObject headers = new JSONObject();
        headers.put(headerAgent, headerAgentArg);
        headers.put(Referer, RefererArg);
        String response ="";
        //获取二维码
        Long now = System.currentTimeMillis();
        try {
            response = HttpUrlConnectionUtil.getQCode(headers, "https://qr.m.jd.com/show?appid=133&size=147&t=" + now);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(voList != null && voList.size()>0) {
            for(EcommerceVO vo : voList){
                JingDongLoginRspBO jdRspBO = new JingDongLoginRspBO();
                BeanUtils.copyProperties(vo,jdRspBO);
                jdRspBO.setImg(response);
                jdRspBOList.add(jdRspBO);
            }
            rspBO.setJdRspBOList(jdRspBOList);
            rspBO.setCode("0");
            rspBO.setMessage("查询成功");
        }else {
            rspBO.setCode("1");
            rspBO.setMessage("查询失败");
        }
        return rspBO;
    }
}
